﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp.View.SystemInformation
{
    /// <summary>
    /// UC_TravellerInformation.xaml 的交互逻辑
    /// </summary>
    public partial class UC_TravellerInformation : UserControl
    {
        public UC_TravellerInformation()
        {
            InitializeComponent();

        }
        BLL.UCTravellerInformation.UC_TravellerInformationClient myTravellerInformation = new BLL.UCTravellerInformation.UC_TravellerInformationClient();
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            GetData();
            Update.IsEnabled = false;//禁用修改按钮
            Update.Opacity = 0.5;
            Delete.IsEnabled = false;//禁用删除按钮
            Delete.Opacity = 0.5;
        }
        /// <summary>
        /// 刷新表格
        /// </summary>
        public void GetData()
        {
            DataTable dt = myTravellerInformation.SelectAllTravellerInformation().Tables[0];
            dgTraveller.ItemsSource = dt.DefaultView;
        }
        /// <summary>
        /// 条件筛选（搜索框）
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txt_Select_SelectionChanged(object sender, RoutedEventArgs e)
        {
            try
            {
                string select = "";
                string strConent = txt_Select.Text.Trim();
                if (strConent != "")
                {
                    //模糊查询内容
                    select += "userName like '%" + strConent + "%'" +
                        "OR certificate_type like'%" + strConent + "%'";

                }
                //全部数据
                DataTable dtAll = myTravellerInformation.SelectAllTravellerInformation().Tables[0];
                DataView dv = new DataView(dtAll);
                //筛选出来的而数据
                DataTable dt = new DataTable();
                if (select != "")
                {
                    //筛选数据
                    dv.RowFilter = select;
                    dt = dv.ToTable();
                }
                if (select == "")
                {
                    //查询全部数据
                    dt = dv.ToTable();
                }
                dgTraveller.ItemsSource = dt.DefaultView;
            }
            catch (Exception)
            {
                return;
            }
        }
        /// <summary>
        /// 新增按钮点击事件
        /// </summary>
        /// <param na
        /// me="sender"></param>
        /// <param name="e"></param>
        private void btnInsert_Click(object sender, RoutedEventArgs e)
        {
            WD_InsertTraveerInformation myWD_InsertStaffAccountManage = new WD_InsertTraveerInformation();
            myWD_InsertStaffAccountManage.ShowDialog();
            GetData();
        }
        /// <summary>
        /// 打开修改窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUpdate_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //获取选中行
                if ((DataRowView)dgTraveller.SelectedItem != null)
                {
                    DataRowView dr = (DataRowView)dgTraveller.SelectedItem;
                    //实例化窗口
                    WD_UpdateTraveerInformation myUpdateTraveerInformation = new WD_UpdateTraveerInformation(dr);
                    //打开窗口
                    myUpdateTraveerInformation.ShowDialog();
                    GetData();
                }
                else
                {
                    MessageBox.Show("请选择要修改的数据");
                }
            }
            catch (Exception)
            {
                return;
            }
        }
        /// <summary>
        /// 选中行（按钮的禁用和启用）
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgTraveer_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                //获取选中行
                if ((DataRowView)dgTraveller.SelectedItem != null)
                {
                    Update.IsEnabled = true;
                    Update.Opacity = 1;
                    Delete.IsEnabled = true;
                    Delete.Opacity = 1;
                }
                else
                {
                    Update.IsEnabled = false;
                    Update.Opacity = 0.5;
                    Delete.IsEnabled = false;
                    Delete.Opacity = 0.5;
                }
            }
            catch (Exception)
            {
                return;
            }


        }
        /// <summary>
        /// 删除旅客信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //获取选中行（当前行）
                if ((DataRowView)dgTraveller.SelectedItem != null)
                {
                    //获取选中行
                    DataRowView drv = (DataRowView)dgTraveller.SelectedItem;
                    //获取主键
                    int intID = Convert.ToInt32(drv.Row["userFileID"]);
                    //执行删除
                    int intCount = myTravellerInformation.DeletePassengerInformation(intID);
                    //判断结果
                    if (intCount > 0)
                    {
                        MessageBox.Show("删除数据成功！");
                        GetData();
                    }
                    else
                    {
                        MessageBox.Show("删除数据失败！");
                    }
                }


            }
            catch (Exception)
            {
                return;
            }


        }
    }
}
